﻿using System.Linq;
using System.Web.Mvc;
using ExtendedMvc.WebTest.Model.Entities;

namespace ExtendedMvc.WebTest.Controllers {
	[HandleError]
	public class HomeController : Controller {
		public ActionResult Detail(long id = 0) {
			var users = MvcApplication.Database.Users;
			var user = users.GetById(id);
			if (id == 0) user = users.All().First();

			return View(user);
		}

		public ActionResult List() {
			var users = MvcApplication.Database.Users;

			return View(users.All());
		}

		public ActionResult Edit(long id = 0) {
			var users = MvcApplication.Database.Users;
			var user = users.GetById(id);
			if (id == 0) user = users.All().First();

			return View(user);
		}

		public ActionResult Update(User model, long id) {
			var users = MvcApplication.Database.Users;
			var user = users.GetById(id);
			if (id == 0) user = users.All().First();

			if (ModelState.IsValid) {
				UpdateModel(user);
				users.Save();
				return RedirectToAction("List");
			}
			return View("Edit", model);
		}


        public ActionResult Partial() {
            return PartialView("Partial");
        }
    }
}
